package test;

import static org.junit.Assert.*;
import hospital.Hospital;

import movement.Location;

import org.junit.Before;
import org.junit.Test;

import unit.*;

import dispatch.DispatchCenter;
import emergency.*;


public class testFireTruck {

	DispatchCenter center;
	FireTruck firetruck;
	Garage garage;
	
    @Before
    public void setUp() throws Exception { 
    	center = new DispatchCenter("DC 1",new Location(45,45,"DC 1 - Location"),new Location(60,60,"GarageLocation"));
    	garage = new Garage(new Location(0,0,"Garage"), center);
    	firetruck = new FireTruck("Firetruck one", 0, garage,Fire.Size.FACILITY);
    }
    
    @Test (expected=IllegalArgumentException.class) 
    public void testAssignHospital(){
		Hospital h = new Hospital("GasthuisBerg", new Location(120,120,"Locatie"));
    	firetruck.assignHospital(h);
	}
    
    @SuppressWarnings("unused")
	@Test (expected=IllegalArgumentException.class) 
    public void testCreateAmbulanceNullGarage(){
    	FireTruck ft = new FireTruck("ft",0,null,Fire.Size.LOCAL);
    	
    }
    
    @SuppressWarnings("unused")
	@Test (expected=IllegalArgumentException.class) 
    public void testCreateAmbulanceNullMaxFiresize(){
    	FireTruck ft = new FireTruck("testing",0,garage,null);
    }
    
    @SuppressWarnings("unused")
	@Test (expected=IllegalArgumentException.class) 
    public void testCreateAmbulanceNullName(){
    	FireTruck ft = new FireTruck(null,0,garage,Fire.Size.LOCAL);
    }
    
    @Test
    public void testGettersAndSetters() {
    	Location secondLocation = new Location(60,60,"SecondLocation");
    	assertEquals(firetruck.isAssigned(),false);
    	assertTrue(firetruck.getCurrentLocation().equals(garage.getLocation()));
    	assertEquals(firetruck.getHomeLocation(), garage);
    	assertEquals(firetruck.getTargetLocation(), null);
    	assertEquals(firetruck.getName(), "Firetruck one");
    	assertEquals(firetruck.getSpeed(),10);
    	assertEquals(firetruck.getMaxFireSize(),Fire.Size.FACILITY);
    	Garage garage2 = new Garage(new Location(10,10,"Epla"), new DispatchCenter("DC", new Location(10,10,"Epla"), new Location(12,320,"Epl2a")));
    	firetruck.setCurrentLocation(secondLocation);
    	firetruck.setHomeLocation(garage2);
    	firetruck.setTargetLocation(secondLocation);
    	firetruck.setName("Firetruck two");
    	firetruck.setSpeed(10);
    	firetruck.setMaxFireSize(Fire.Size.HOUSE);
    	
    	assertTrue(firetruck.getCurrentLocation().equals(secondLocation));
    	assertEquals(firetruck.getHomeLocation(), garage2);
    	assertEquals(firetruck.getTargetLocation(), secondLocation);
    	assertEquals(firetruck.getName(), "Firetruck two");
    	assertEquals(firetruck.getSpeed(),10);
    	assertEquals(firetruck.getMaxFireSize(),Fire.Size.HOUSE);
    }

	
	@Test (expected=IllegalAccessException.class)
	public void testRevokeInvalidFiretruck() throws IllegalAccessException{
		Unit u4 = new FireTruck("Firetruck-1", 90, garage, Fire.Size.FACILITY);
		// can't revoke firetruck 
		u4.revokeUnit();
	}
	
	@Test (expected=IllegalArgumentException.class) 
    public void testSetInvalidCurrentLocation(){
    	firetruck.setCurrentLocation(null);
    }
	
	@Test (expected=IllegalArgumentException.class) 
    public void testSetInvaliHomeLocation(){
    	firetruck.setHomeLocation(null);
	}
	
	
	@Test
    public void testSetNegativeSpeed(){
    	firetruck.setSpeed(-20);
    	assertEquals(10, firetruck.getSpeed());
    }
	
	@Test 
	public void testToString(){
		assertEquals("FireTruck-Firetruck one",firetruck.toString());
	}
	
	@SuppressWarnings("unused")
	@Test
	public void testUnitOfSameTypeOnEmergency() throws IllegalAccessException{
		FireTruck ft2 = new FireTruck("FT2", 50, garage, Fire.Size.HOUSE);
		PoliceCar pc = new PoliceCar("PC2", 50, garage);
		Ambulance am = new Ambulance("AM1", 50, garage);
		Ambulance amb = new Ambulance("AM2", 50, garage);
		Location loc = new Location(36,36,"Loc");
		
		Emergency em = new Fire(loc, Severity.SERIOUS, center, Fire.Size.HOUSE, false, 1, 1);

		em.assignAllUnits(garage);
		
		//fails if not all firetrucks are on the emergency
		assertFalse(firetruck.unitsOfSameTypeOnEmergency());
		
		firetruck.setCurrentLocation(em.getLocation());
		
		assertFalse(firetruck.unitsOfSameTypeOnEmergency());
		
		ft2.setCurrentLocation(em.getLocation());
		
		//returns true if all firetrucks are on the emergency
		assertTrue(firetruck.unitsOfSameTypeOnEmergency());

	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testUnitOfSameTypeOnEmergencyNoEmergency(){
		firetruck.unitsOfSameTypeOnEmergency(); // firetruck doesn't have an emergency yet 
	}
}
